/**
 * Created With IntelliJ IDEA
 * Description:leetcode:1455. 检查单词是否为句中其他单词的前缀
 * <a href="https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/">...</a>
 * User: DELL
 * Data: 2023-03-31
 * Time: 23:42
 */
public class Solution {
    public int isPrefixOfWord(String sentence, String searchWord) {
        //题目中规定字符串均不为空，且长度大于1
        int len = sentence.length() - searchWord.length();
        if (len < 0) {
            return -1;
        }
        //判断第一个单词
        for (int i = 0; i < searchWord.length(); i++) {
            if (sentence.charAt(i) != searchWord.charAt(i)) {
                break;
            }
            if (i == searchWord.length()-1) {
                return 1;
            }
        }
        int res = 0;
        for (int i = 0; i < len; i++) {
            if (sentence.charAt(i) == ' ') {
                res++;
                int j = 1;
                for (; j <= searchWord.length(); j++) {
                    if (sentence.charAt(i+j) != searchWord.charAt(j-1)) {
                        break;
                    }
                }
                if (j == searchWord.length()+1) {
                    return res+1;
                }
            }
        }
        return -1;
    }
}